﻿using HtmlAgilityPack;

namespace apitest.Application.ERPTable
{
    public class ERP_TableService 
    {
        public readonly ISqlSugarClient _db;
        public ERP_TableService(ISqlSugarClient db)
        {
            _db = db;
        }

        [HttpGet]
        public async Task<dynamic> T1()
        {


            var path = @"供应链.html";
            var doc = new HtmlDocument();
            doc.Load(path);
            var h4 = doc.DocumentNode.SelectNodes("//h4");

            var h5 = doc.DocumentNode.SelectNodes("//h5");
            foreach (var item in h5)
            {
                var text = item.InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", "");
                if (text == "触发器")
                {
                    item.Remove();
                }
            }
            h5 = doc.DocumentNode.SelectNodes("//h5");


            var list = new List<string>()
            {
                "概述",
                "字段",
                "索引",
                "主外键",
                "约束",
            };

            var tables = doc.DocumentNode.SelectNodes("//table");
            Console.WriteLine($"h5:{h5.Count},tables:{tables.Count}");
            var l = new List<string>();
            var t = new List<string>();
            var i = 0;
            foreach (var item in h5)
            {
                if (i == 5)
                {
                    await Console.Out.WriteLineAsync(string.Join(",", t));
                    l.Add(string.Join(",", t));
                    t = new List<string>();
                    i = 0;
                }
                i += 1;
                var text = Handler(item.InnerText);
                t.Add(text);
                //await Console.Out.WriteLineAsync(Handler(item.InnerText) );
            }
            foreach (var node in h4)
            {
                if (node.InnerHtml.Contains("基本信息"))
                {
                    var tbs = doc.DocumentNode.SelectNodes("/html[1]/body[1]/div[1]/div[2]/table[6141]");
                    return tbs[0].InnerHtml;
                    return node.XPath;
                    return node.InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", "");
                }
            }

            //var html = @"http://172.27.160.206:87/333.html";
            //HtmlWeb web = new HtmlWeb();
            //var htmlDoc = web.Load(html);
            //var h2= htmlDoc.DocumentNode.SelectNodes("//h2");
            //var h4 = htmlDoc.DocumentNode.SelectNodes("//h4");
            //var table = htmlDoc.DocumentNode.SelectNodes("//table");
            //var h5 = htmlDoc.DocumentNode.SelectNodes("//h5");
            return "";

        }

        private string Handler(string str)
        {
            return str.Replace("\r", "").Replace("\t", "").Replace("\n", "");
        }
    }
}
